package com.springboot.controller.interceptor;

import com.springboot.pojo.Employee;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MvcInterceptor  implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("调用preHandle方法....");
        HttpSession session = request.getSession();
        //统一拦截（查询当前session是否存在loginBean）(这里loginBean会在每次登录成功后，写入session)
            Employee employee = (Employee) session.getAttribute("employee");
        if (employee != null) {
            return true;
        }
       // response.sendRedirect(request.getContextPath() + "/backend/page/login/login.html");
        System.out.println("----");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
